package com.xinzhi.service.impl;

import com.xinzhi.dao.UserMapper;
import com.xinzhi.entity.Man;
import com.xinzhi.entity.Result;
import com.xinzhi.entity.User;
import com.xinzhi.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Result getInfo(int id, HttpServletRequest request) {
        User user = userMapper.getInfo(id);
        if (user != null){
            Map<String,String> tokenMap = new HashMap<>();
            tokenMap.put("tname",user.getTname());
            tokenMap.put("headhost",user.getHeadhost());
            tokenMap.put("balance",""+user.getBalance());
            return Result.ok(tokenMap);
        }
        return Result.error(5003,"用户信息不存在");
    }

    @Override
    public Result uptname(String tname, int id, HttpServletRequest request) {
        if (tname.isEmpty()){
            return Result.error(5008,"输入不能为空");
        }else {
            int count = userMapper.uptname(tname, id);
            if (count > 0) {
                return Result.ok(tname);
            }
            return Result.error(5004, "修改失败");
        }
    }

    @Override
    public Result upheadhost(String headhost, int id, HttpServletRequest request) {
        if (headhost.isEmpty()){
            return Result.error(5008,"输入不能为空");
        }
        int count = userMapper.upheadhost(headhost,id);
        if (count > 0){
            return Result.ok(headhost);
        }
        return Result.error(5004,"修改失败");
    }

    @Override
    public Result recharge(int id,BigDecimal money) {
        //判断用户输入充值金额大于0
        if (money.compareTo(BigDecimal.ZERO) == 1){
            //查询用户余额
            BigDecimal balance = userMapper.balance(id);
            //将用户充值金额与余额相加存入数据库中
            int count = userMapper.upbalance(id,money.add(balance));
            if (count > 0){
            return Result.ok(money.add(balance));
            } else {
              return Result.error(5005,"修改失败");
            }
        }
        return Result.error(5005,"充值金额需要大于0");
    }

    @Override
    public Result inMan(Integer userid, String name, String phone) {
        if (name.isEmpty() && phone.isEmpty()){
            return Result.error(5008,"输入不能为空");
        }
        int count = userMapper.inMan(userid,name,phone);
        if (count > 0){
            Map<String,String> tokenMap = new HashMap<>();
            tokenMap.put("name",name);
            tokenMap.put("phone",phone);
            return Result.ok(tokenMap);
        }
        return Result.error(5004,"添加失败");
    }

    @Override
    public Result getMan(Integer id) {
        List<Man> list = userMapper.getMan(id);
        if (list.size() >= 0){
            return Result.ok(list);
        }
        return Result.error(5005,"系统错误");
    }

    @Override
    public Result deMan(Integer id) {
        int count = userMapper.deMan(id);
        if (count > 0){
            return Result.ok(id);
        }
        return Result.error(5006,"删除失败");
    }

    @Override
    public Result upMan(Integer id, String name, String phone) {
        if (name.isEmpty() && phone.isEmpty()){
            return Result.error(5008,"输入不能为空");
        }
        int count = userMapper.upMan(id,name,phone);
        if (count > 0){
            Map<String,String> tokenMap = new HashMap<>();
            tokenMap.put("id",id+"");
            tokenMap.put("name",name);
            tokenMap.put("phone",phone);
            return Result.ok(tokenMap);
        }
        return Result.error(5004,"修改失败");
    }
}
